


local file_path_in    "/tochange/"

timer clear 1
timer on 1

clear 
clear matrix
clear mata

set maxvar 32000
set matsize 11000
set more off 

** regtype refers to the restrictions put on the sample in the regression

local regtype `1'  
local reghetero `2'
local regtimefe `3'
local regplacebo `4'
local regoutcome `5'
local fin_exploration `6'
local regsizeplot `7'
local outcomeyr `8'
local regcontrols `9'
local hpcontrol `10'  /* include level of house prices, or changes */
local reglevel `11'    /* will be either "zip" or "fips"  */
local clustervar `12'
local inclpublic `13'
local regzipquality `14'
local houscharhetero `15'
local startyear `16'

local regoutcomeyr ""
if "`outcomeyr'"=="09" {
	local regoutcomeyr "keep if year==2009"
}
if "`outcomeyr'"=="10" {
	local regoutcomeyr "keep if year==2010"
}
if "`outcomeyr'"=="08" {
	local regoutcomeyr "keep if year==2008"
}
if "`outcomeyr'"=="crisisyrs" {
	local regoutcomeyr "keep if year==2009 | year==2010 | year==2011 | year==2012"
}

else if "`regnosmall'"=="bigfips" {
	local bigfips "replace shockdelta1yrZIL_`reglevel' = 0 if nfipsyr<=2  & !mi(shockdelta1yrZIL_`reglevel')"
}

local shock "shockdelta1yrZIL_`reglevel'"

** sample construction
use "`file_path_in'/HOUSEBUILDERshockHHI.dta", clear
keep if error==0
keep if small==0
drop if nfipsyr<=2
drop if land_sale==1


tostring FIPS_CODE, gen(fips_string)
gen fstr = strlen(fips_string)
replace fips_string = "0"+fips_string if fstr==4
gen statefp = substr(fips_string,1,2)
destring statefp, replace

** calculate number of homes sold per builder
bysort seller year: gen nhomes = _n
bysort seller year: replace nhomes=nhomes[_N]

** calculate number of homes sold per builder in 2006
g nhomes_06 = nhomes if year==2006
bysort seller: egen temp = max(nhomes_06)
drop nhomes_06 
rename temp nhomes_06

** calculate number of homes sold per builder in 2009
g nhomes_09 = nhomes if year==2009
bysort seller: egen temp = max(nhomes_09)
drop nhomes_09 
rename temp nhomes_09
	
** calculate number of states per builder
bysort seller year state: gen n_state= _n==1
bysort seller year: gen nstate = sum(n_state)
bysort seller year: replace nstate=nstate[_N]	

** calculate number of counties per builder
bysort seller year FIPS_CODE: gen n_fips= _n==1
bysort seller year: gen nfips = sum(n_fips)
bysort seller year: replace nfips=nfips[_N]
	
if "`regtype'"=="main09"  {
	drop shockdelta1yrZIL_`reglevel'
	rename shockdelta0609ZIL_`reglevel' shockdelta1yrZIL_`reglevel'
}


** drop if shock is 0
drop if shockdelta1yrZIL_`reglevel'==0

** merge in financials
merge m:1 seller year quarter using "`file_path_in'/financialsq.dta", keep(master match)
gen publicbuilder = 1 if _merge==3
replace publicbuilder = 0 if mi(publicbuilder)
bysort seller: egen publicbuildermax = max(publicbuilder)
drop _merge
	
label var shockdelta1yrZIL_`reglevel' "shock"
replace sqfeet = sqfeet*1000 

if "`regplacebo'"==""  & "`outcomeyr'"=="09" {
	keep if year==2009 
}		
** sale price
gen lnsaleamt = log(saleamount)
label var lnsaleamt "Log Sale Price"

gen sf = PROPERTY_INDICATOR_CODE==10
gen condo = PROPERTY_INDICATOR_CODE==11
gen duplex = PROPERTY_INDICATOR_CODE==12

rename FIPS_CODE fips

** label vars
label var nfipsyr "No. Counties per Yr"
label var shockdelta1yrZIL_`reglevel' "shock"
label var saleamount "Sale Price"
label var lnsaleamt "Log Sale Price"
label var sqfeet "Sq. Ft."
label var yearbuilt "Year Built"
label var baths "No. Baths"
label var sf "Single Family"
label var condo "Condo"
label var duplex "Duplex"
label var rooms "Rooms"
label var ltv "LTV"
label var hbloan "Builder Mortgage"
label var shockdelta1yrZIL_`reglevel' "Shock"
label var mortgage_term "Mortgage Term"

**** homes summary statistics **** 

if  "`regtype'"=="main09"  {	
	
	`regoutcomeyr'   // usually will be, "keep if year==2009"
	
	** 2 columns, split by public and all firms
	eststo all: quietly estpost summarize   saleamount sqfeet baths rooms condo duplex sf mortgage_term hbloan if !mi(shockdelta1yrZIL_fips) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE), d
	eststo public: quietly estpost summarize  saleamount sqfeet baths rooms condo duplex sf mortgage_term hbloan if !mi(shockdelta1yrZIL_fips) & !mi(sqfeet) & !mi(baths) & !mi(PROPERTY_INDICATOR_CODE) & coverage_q06_mean!=. ,d
	esttab all public using "`file_path_in'/reg output/summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.tex",  ///
		cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) p50(pattern(1 1 0) fmt(2))") label nonum replace mtitles("All Firms" "Public Firms")
	esttab all public using "`file_path_in'/reg output/summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.csv", ///
		cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) p50(pattern(1 1 0) fmt(2))") label nonum replace mtitles("All Firms" "Public Firms")


**** builder summary statistics **** 

	`regoutcomeyr'  // "keep if year==2009" usually
		
		keep if !mi(shockdelta1yrZIL_`reglevel') &  !mi(hpZIL_`reglevel') & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE)
		
		* builder dispersion: builder hhi
		bysort seller year state: gen homes_state = _n
		bysort seller year state: replace homes_state=homes_state[_N]
		bysort seller year state: gen share = homes_state / nhomes
		
		tempfile temp
		preserve 
			collapse share, by(seller year state) 
			gen share_sq = share^2
			bysort seller year : gen hhi = sum(share_sq)
			bysort seller year : replace hhi = hhi[_N]
			save `temp', replace
		restore
		merge m:1 seller year state using `temp', nogen
		sum hhi 
		label var hhi "HHI"
	
		* summarize split by publics
		preserve 
			collapse hhi nhomes nstate nfips leverage_q06_mean coverage_q06_mean pcteq_subsassets_a06 equity_unconsolsub_a06 pctcash_a06 pctoption_a06 assets_a06 profitability_a06 mtb_q06 fixed_a06 ///
				if !mi(shockdelta1yrZIL_`reglevel') & !mi(hpZIL_`reglevel') & !mi(baths) & !mi(sqfeet) & !mi(PROPERTY_INDICATOR_CODE) , by(publicbuildermax seller year)	
			isid seller year
			
			label var hhi "\$Geographic HHI_{09}\$"
			label var nhomes "\$No. Homes Sold_{09}\$"
			label var nstate "\$No. States_{09}\$"
			label var nfips "\$No. Counties_{09}\$"
			label var leverage_q06_mean "\$ Leverage_{06} \$"
			label var coverage_q06_mean "\$ Coverage_{06} \$"
			label var pcteq_subsassets_a06 "\$ \% Equity in Subsidiaries_{06} \$"
			label var pctcash_a06 "\$ Cash to Assets_{06} \$"
			label var pctoption_a06 "\$ \% Land Held via Option_{06} \$"
			label var fixed_a06 "\$ Tangibility_{06} \$"
			label var profitability_a06 "\$ Profitability_{06} \$"  
			label var mtb_q06 "\$ Market to Book_{06} \$"
			label var assets_a06 "\$ Assets_{06} (\$ Mil.)\$"
			
			cap label drop public
			label define public 0 "Private" 1 "Public"
			label values publicbuildermax public
			eststo clear
			
			eststo all: quietly estpost summarize nhomes nstate hhi , d	
			eststo public: quietly estpost summarize nhomes nstate hhi leverage_q06_mean coverage_q06_mean pcteq_subsassets_a06 pctcash_a06 pctoption_a06 assets_a06 profitability_a06 mtb_q06 fixed_a06 if coverage_q06_mean!=. , d
			esttab all public using "`file_path_in'/reg output/bldr_summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.csv", ///
				cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) p50(pattern(1 1 0) fmt(2)) ") label mtitles("All Firms" "Public Firms") nonum replace substitute(\_ _)
			esttab all public using "`file_path_in'/reg output/bldr_summ_stats_`regtype'`outcomeyr'`reglevel'twocolumns.tex", ///
				cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) p50(pattern(1 1 0) fmt(2)) ") label mtitles("All Firms" "Public Firms") nonum replace substitute(\_ _)

		restore
}

timer off 1
timer list 1
